package com.powernode.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.IndexImgConstant;
import com.powernode.domain.IndexImg;
import com.powernode.domain.Prod;
import com.powernode.feign.IndexImgProdFeign;
import com.powernode.mapper.IndexImgMapper;
import com.powernode.service.IndexImgService;
import com.powernode.utils.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "com.powernode.service.impl.IndexImgServiceImpl")
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService {

    @Autowired
    private IndexImgMapper indexImgMapper;


    @Autowired
    private IndexImgProdFeign indexImgProdFeign;

    /**
     * 分页查询轮播图管理
     *
     * @param page
     * @param indexImg
     * @return
     */
    @Override
    public Page<IndexImg> selectIndexImgPage(Page<IndexImg> page, IndexImg indexImg) {
        Long shopId = AuthUtil.getUserIdOrShopId(false);
        return indexImgMapper.selectPage(page, new LambdaQueryWrapper<IndexImg>()
                .eq(shopId != 1, IndexImg::getShopId, shopId)
                .eq(!ObjectUtils.isEmpty(indexImg.getStatus()), IndexImg::getStatus, indexImg.getStatus())
                .orderByDesc(IndexImg::getSeq)
        );
    }

    /**
     * 新增轮播图
     *
     * @param indexImg
     */
    @Override
    @CacheEvict(key = IndexImgConstant.INDEX_IMG_FRONT)
    public void saveIndexImg(IndexImg indexImg) {
        Long userId = AuthUtil.getUserIdOrShopId(true);
        log.info("新增轮播图{}，用户id为{}，店铺id为{}", JSON.toJSONString(indexImg), userId, indexImg.getShopId());
        indexImg.setUploadTime(new Date());
        indexImgMapper.insert(indexImg);
    }


    /**
     * 回显轮播图
     * 1.先查询轮播图
     * 2.拿到商品id
     * 3.判断 发起远程调用 查询商品的名字和图片
     *
     * @param id
     * @return
     */
    @Override
    public IndexImg selectIndexImgInfo(Long id) {
        IndexImg indexImg = indexImgMapper.selectById(id);
        Long prodId = indexImg.getRelation();
        if (!ObjectUtils.isEmpty(prodId)) {
            // 发起远程调用 查询商品的名字和图片
            Prod prod = indexImgProdFeign.getProdInfoByProdId(prodId);
            // 外面开发 远程调用完以后 要先判空
            // 我们一般要考虑实际情况  判断这个调用后的结果 对下面的代码有没有影响
            // 如果有影响  直接报错  如果没有影响 不管
            if (!ObjectUtils.isEmpty(prod)) {
                indexImg.setProdName(prod.getProdName());
                indexImg.setPic(prod.getPic());
            }
        }
        return indexImg;
    }


    /**
     * 加载前台商城的轮播图
     *
     * @return
     */
    @Override
    @Cacheable(key = IndexImgConstant.INDEX_IMG_FRONT)
    public List<IndexImg> selectFrontIndexImg() {
        return indexImgMapper.selectList(new LambdaQueryWrapper<IndexImg>()
                .select(IndexImg::getImgUrl, IndexImg::getRelation)
                .eq(IndexImg::getStatus, 1)
                .orderByDesc(IndexImg::getSeq)
                .last("limit 5") // last 就是你可以自定义sql语句
        );
    }
}
